import sys
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from  PyQt5.QtSql import *
def initModel(model):
    model.setTable('student')
    model.setEditStrategy(QSqlTableModel.OnFieldChange)
    model.select()
    model.setHeaderData(0,Qt.Horizontal,"id")
    model.setHeaderData(1, Qt.Horizontal, "name")
    model.setHeaderData(2, Qt.Horizontal, "sex")
def createView(title,model):
    view=QTableView()
    view.setModel(model)
    view.setWindowTitle(title)
    return view
def findrow(i):
    delrow=i.row()
    print(str(delrow))
def addRow():
    ret=model.insertRows(model.rowCount(),1)
    print(str(ret))

if __name__ == '__main__':
    app=QApplication(sys.argv)

    db=QSqlDatabase.addDatabase('QSQLITE')
    db.setDatabaseName('./sudent.db')
    model=QSqlTableModel()
    delrow=-1
    initModel(model)
    view1=createView('table Model view',model)
    view1.clicked.connect(findrow)
    dlg=QDialog()
    layout=QVBoxLayout()
    layout.addWidget(view1)
    addbtn=QPushButton('添加一行')
    addbtn.clicked.connect(addRow)
    layout.addWidget(addbtn)

    delBtn=QPushButton('删除一行')
    delBtn.clicked.connect(lambda :model.removeRow(view1.currentIndex().row()))
    layout.addWidget(delBtn)
    dlg.setLayout(layout)
    dlg.setWindowTitle('data')
    dlg.resize(400,500)
    dlg.show()
    sys.exit(app.exec_())

    sys.exit(app.exec_())